<?php
/**
 * Klasse zum Aufbau und Validierung von Formularen
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


Class Yapary_Form_Formular_Row
{
	/* @var $arrElement Yapary_Form_Formular_Element[] */
	private $arrElement		= array();
	private $method			= NULL;
	private $desc			= NULL;
	private $enableDesc		= true;
	private $enableRowColor	= false;
	private $hidden			= NULL;


	/**
	 * @param string $_method	: post|get
	 * @param string $_desc		: Beschreibung
	 * @param string $hidden	: Wenn Hidden-Text angegeben wird, wird das Formular geschlossen angezeigt und per Klick auf Text geoeffnet
	 * */
	function __construct ($_desc = '', $_hiddenText = NULL) {
		$this->desc = $_desc;
		$this->hidden = $_hiddenText;
	}


	/**
	 * Setzt die Beschreibung.
	 * @param string $_desc		: Beschreibung
	 * @return void
	 * */
	public function setDesc ($_desc) {
		$this->desc = $_desc;
	}


	/**
	 * Liefert die Beschreibung zurueck.
	 * @return string
	 * */
	public function getDesc () {
		return $this->desc;
	}


	/**
	 * Liefert eine Zeile zurueck.
	 * @param int $i	: Auszugebende Zeile
	 * @return Yapary_Form_Formular_Element
	 * */
	public function addElement ($_element) {
		return $this->arrElement[] = new Yapary_Form_Formular_Element($_element);
	}


	/**
	 * Liefert eine Zeile zurueck.
	 * @param int $i	: Auszugebende Zeile
	 * @return Yapary_Form_Formular_Element
	 * */
	public function addTemplate ($_element) {
		return $this->arrElement[] = new Yapary_Form_Formular_Template($_element);
	}


	/**
	 * Liefert eine Zeile zurueck.
	 * @param int $i	: Auszugebende Zeile
	 * @return Yapary_Form_Formular_Element
	 * */
	public function getElement ($i) {
		if (!isset($this->arrElement[$i])) {
			return NULL;
		}

		return $this->arrElement[$i];
	}


	/**
	 * @desc Liefert alle Zeilen zurueck.
	 * @return Yapary_Form_Formular_Element[]
	 * */
	public function getElementList () {
		return $this->arrElement;
	}


	/**
	 * Loescht einen Eintrag.
	 * @param string $id	: Zu loeschender Eintrag
	 * @return bool
	 * */
	public function deleteElement ($i) {
		if (!isset($this->arrElement[$i])) {
			return false;
		}

		unset($this->arrElement[$i]);
		return true;
	}


	/**
	 * @desc Gibt an, ob das Formular bereits versendet wurde.
	 * @return bool
	 * */
	public function hasSend () {
		$valid = true;

		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->hasSend() === false) {
					$valid = false;
				}
			}
		}

		return $valid;
	}


	/**
	 * @desc Gibt an, ob das Formular bereits versendet wurde.
	 * @return bool
	 * */
	public function hasError () {
		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->hasError() === true) {
					return true;
				}
			}
		}

		return false;
	}


	/**
	 * @desc Liefert alle entstandenen Fehler.
	 * @return array
	 * */
	public function getErrorList () {
		$arrError = array();

		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				$arrError[] = $oElement->getError();
			}
		}

		return $arrError;
	}


	/**
	 * @desc Liefert die Anzahl an internen und externen Fehlern.
	 * @return int
	 * */
	public function getErrorCount () {
		$count = 0;

		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->hasError()) {
					$count++;
				}
			}
		}

		return $count;
	}


	/**
	 * @desc Gibt an, dass Fehlercodes aus der Validierung angezeigt werden sollen
	 * @return void
	 * */
	public function enableErrorReporting ($_enable = true) {
		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				$oElement->enableErrorReporting($_enable);
			}
		}
	}


	/**
	 * @desc Gibt an, dass Beschreibugnstexte angezeigt werden sollen
	 * @return void|boolean
	 * */
	public function enableDesc ($_enable = NULL) {
		if (gettype($_enable) == 'boolean') { // Wert setzen
			$this->enableDesc = $_enable;
		} else { // Wert auslesen
			return $this->enableDesc;
		}
	}


	/**
	 * @desc Gibt an, dass Beschreibugnstexte angezeigt werden sollen
	 * @return void|boolean
	 * */
	public function enableRowColor ($_enable = NULL) {
		if (gettype($_enable) == 'boolean') { // Wert setzen
			$this->enableRowColor = $_enable;
		} else { // Wert auslesen
			return $this->enableRowColor;
		}
	}


	/**
	 * @desc Validiert alle Formulareingaben.
	 * @return int|NULL	: Validierung erfolgreich=true, sonst false
	 * */
	public function validate () {
		$valid = true;

		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->validate() === false) {
					$valid = false;
				}
			}
		}

		return $valid;
	}


	/**
	 * @desc Fuegt dem Element elName einen externen Fehler hinzu.
	 * @return string $elName	: Elementname
	 * @return string $_error	: Fehlermeldung
	 * */
	public function setExternalError ($elName, $_error) {
		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->getName() == $elName) {
					$oElement->setExternalError($_error);
					return true;
				}
			}
		}

		return false;
	}


	/**
	 * @desc Gibt das Formular aus.
	 * @return string
	 * */
	public function display() {
		$row = '';
		$count = count($this->arrElement);

		// Hidden-Definitionen
		$hiddenDataClass	= ($this->hidden != NULL) ? ' Yapary_Form_Formular_rowHiddenData' : '';
		$hiddenRowClass		= ($this->hidden != NULL) ? ' Yapary_Form_Formular_rowHidden' : '';
		$hiddenClick		= ($this->hidden != NULL) ? '<span class="Yapary_Form_Formular_rowHiddenTxt">'.$this->hidden.'</span>' : '';

		if ($count) {
			$size = ' Yapary_Form_Formular_Element_size_1_'.$count;
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				$formValid	= ($oElement->hasError()) ? ' Yapary_Form_Formular_invalid' : ' Yapary_Form_Formular_valid';
				$err		= ($oElement->enableErrorReporting() === true) ? '<span class="Yapary_Form_Formular_Error">'.$oElement->getError().'</span>' : '';

				// Wenn Fehler ausgetreten ist und Element als Hidden gekennzeichnet, dann zeige Fehlerhaftes Element trotz Hidden-Kennzeichnung an
				if ($oElement->getError() != NULL) {
					$hiddenDataClass = '';
					$hiddenRowClass = '';
					$hiddenClick = '';
				}

				$row .=	'<div class="Yapary_Form_Formular_row_element '.$formValid.$size.'">
							'.$oElement->getElement().$err.'</span>
						</div>';
			}

			$formColor = ($this->enableRowColor() === true) ? ' Yapary_Form_Formular_row_color' : '';

			$desc = '<div class="Yapary_Form_Formular_row_desc">
						'.$this->desc.'
					</div>';

			$row = '<div class="Yapary_Form_Formular_row_data '.$hiddenDataClass.$formColor.'">'.$row.'</div>';
		} else {
			$desc = $this->desc;
		}


		return '<div class="control-group">
				    <label class="control-label" for="datum">'.$this->desc.'</label>
				    <div class="controls form-inline '.$hiddenRowClass.'">
						'.$hiddenClick.$row.'
					</div>
				</div>
		';

// 		if ($this->enableDesc === true) {
// 			return '<div class="Yapary_Form_Formular_row'.$hiddenRowClass.'">
// 						'.$hiddenClick.$desc.$row.'
// 					</div>';
// 		} else {
// 			return '<div class="Yapary_Form_Formular_row">
// 						'.$row.'
// 					</div>';
// 		}
	}


}


?>